package com.stephentuso.welcome;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.utils.PacMap;

/**
 * Created by stephentuso on 11/15/15.
 * Class that handles showing a welcome screen
 */
public class WelcomeHelper {

    public static final int DEFAULT_WELCOME_SCREEN_REQUEST = 1;

    private static final String KEY_WELCOME_SCREEN_STARTED = "com.stephentuso.welcome.welcome_screen_started";

    private Ability mAbility;
    private Class<? extends WelcomeAbility> mAbilityClass;
    private boolean welcomeScreenStarted = false;

    /**
     * @param ability An ability
     * @param abilityClass Class of your welcome screen. An Ability that extends {@link WelcomeAbility WelcomeAbility}
     */
    public WelcomeHelper(Ability ability, Class<? extends WelcomeAbility> abilityClass) {
        mAbility = ability;
        mAbilityClass = abilityClass;
    }

    private boolean getWelcomeScreenStarted(PacMap savedInstanceState) {
        if (!welcomeScreenStarted) {
            welcomeScreenStarted = savedInstanceState != null && savedInstanceState.getBooleanValue(KEY_WELCOME_SCREEN_STARTED, false);
        }
        return welcomeScreenStarted;
    }

    private boolean shouldShow(PacMap savedInstanceState) {
        return !getWelcomeScreenStarted(savedInstanceState) &&
                !WelcomeSharedPreferencesHelper.welcomeScreenCompleted(mAbility, WelcomeUtils.getKey(mAbilityClass));
    }

    /**
     * Shows the welcome screen if it hasn't already been started or completed yet
     * @param savedInstanceState Saved instance state Bundle
     * @return true if the welcome screen was shown, false if it wasn't
     */
    public boolean show(PacMap savedInstanceState) {
        return show(savedInstanceState, DEFAULT_WELCOME_SCREEN_REQUEST);
    }

    /**
     * Shows the welcome screen if it hasn't already been started or completed yet
     * @param savedInstanceState Saved instance state Bundle
     * @param requestCode The request code that will be returned with the result of the welcome screen
     *                    in your Ability's onAbilityResult
     * @return true if the welcome screen was shown, false if it wasn't
     */
    public boolean show(PacMap savedInstanceState, int requestCode) {
        boolean shouldShow = shouldShow(savedInstanceState);
        if (shouldShow) {
            welcomeScreenStarted = true;
            startAbility(requestCode);
        }
        return shouldShow;
    }

    /**
     * Always shows the welcome screen
     */
    public void forceShow() {
        forceShow(DEFAULT_WELCOME_SCREEN_REQUEST);
    }

    /**
     * Always show the welcome screen
     * @param requestCode The request code that will be returned with the result of the welcome screen
     *                    in your Ability's onAbilityResult
     */
    public void forceShow(int requestCode) {
        startAbility(requestCode);
    }

    public void onSaveInstanceState(PacMap outState) {
        outState.putBooleanValue(KEY_WELCOME_SCREEN_STARTED, welcomeScreenStarted);
    }

    private void startAbility(int requestCode) {
        Operation operation = new Intent.OperationBuilder()
                .withBundleName(mAbility.getAbilityPackage().getBundleName())
                .withAbilityName(mAbilityClass)
                .build();
        Intent intent = new Intent();
        intent.setOperation(operation);
        mAbility.startAbilityForResult(intent, requestCode);
    }

}
